package org.xxpay.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.exception.ServiceException;
import org.xxpay.core.entity.PayDivisionMch;
import org.xxpay.core.entity.PayDivisionMchExample;
import org.xxpay.core.service.IPayDivisionMchService;
import org.xxpay.service.dao.mapper.AgentInfoMapper;
import org.xxpay.service.dao.mapper.MchInfoMapper;
import org.xxpay.service.dao.mapper.PayDivisionMchMapper;

import java.util.List;

@Service
public class PayDivisionMchServiceImpl implements IPayDivisionMchService {

    @Autowired
    private PayDivisionMchMapper recordMapper;

    @Autowired
    private MchInfoMapper mchInfoMapper;

    @Autowired
    private AgentInfoMapper agentInfoMapper;


    @Override
    public List<PayDivisionMch> select(int offset, int limit, PayDivisionMch condition) {
        PayDivisionMchExample example = new PayDivisionMchExample();
        example.setOrderByClause("CreateTime desc");
        example.setOffset(offset);
        example.setLimit(limit);
        PayDivisionMchExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, condition);
        return recordMapper.selectByExample(example);
    }

    @Override
    public Integer count(PayDivisionMch condition) {
        PayDivisionMchExample example = new PayDivisionMchExample();
        PayDivisionMchExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, condition);
        return recordMapper.countByExample(example);
    }

    @Override
    public PayDivisionMch findById(Long id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(PayDivisionMch PayDivisionMch) {

        if(MchConstant.SETT_INFO_TYPE_MCH == PayDivisionMch.getInfoType()){
            if(mchInfoMapper.selectByPrimaryKey(PayDivisionMch.getInfoId()) == null){
                throw new ServiceException(RetEnum.RET_SERVICE_MCH_NOT_EXIST);
            }
        }else if(MchConstant.SETT_INFO_TYPE_AGENT == PayDivisionMch.getInfoType()){
            if(agentInfoMapper.selectByPrimaryKey(PayDivisionMch.getInfoId()) == null){
                throw new ServiceException(RetEnum.RET_SERVICE_AGENT_NOT_EXIST);
            }
        }

        return recordMapper.insertSelective(PayDivisionMch);
    }

    @Override
    public int update(PayDivisionMch PayDivisionMch) {
        return recordMapper.updateByPrimaryKeySelective(PayDivisionMch);
    }

    @Override
    public String getChannelMchId(Byte infoType, Long infoId, String ifTypeCode) {

        PayDivisionMchExample exa = new PayDivisionMchExample();
        exa.createCriteria()
        .andInfoTypeEqualTo(infoType).andInfoIdEqualTo(infoId).andIfTypeCodeEqualTo(ifTypeCode);

        List<PayDivisionMch> result = recordMapper.selectByExample(exa);
        return result.isEmpty() ? null : result.get(0).getChannelMchId();
    }


    void setCriteria(PayDivisionMchExample.Criteria criteria, PayDivisionMch obj) {
        if(obj != null) {
            if(obj.getId() != null ) criteria.andIdEqualTo(obj.getId());
            if(StringUtils.isNotEmpty(obj.getIfTypeCode())) criteria.andIfTypeCodeEqualTo(obj.getIfTypeCode());
            if(obj.getInfoId() != null) criteria.andInfoIdEqualTo(obj.getInfoId());
            if(obj.getInfoType() != null) criteria.andInfoTypeEqualTo(obj.getInfoType());
            if(StringUtils.isNotEmpty(obj.getChannelMchId())) criteria.andChannelMchIdEqualTo(obj.getChannelMchId());
        }
    }

}
